package com.manage.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.platform.context.UserContext;
import com.manage.platform.dto.TaskAchieveDetailsDto;
import com.manage.platform.entity.TaskAchieveEntity;
import com.manage.platform.mapper.TaskAchieveMapper;
import com.manage.platform.vo.CommonVo;
import com.manage.platform.vo.TaskAchieveDetailsVo;
import com.manage.platform.vo.UserVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * YWD
 * 2025/11/24
 */
@AllArgsConstructor
@Service
public class TaskAchieveServiceImpl extends ServiceImpl<TaskAchieveMapper, TaskAchieveEntity> {

    private final TaskAchieveFileServiceImpl taskAchieveFileServiceImpl;
    private final TaskAchieveFileLinkServiceImpl taskAchieveFileLinkServiceImpl;

    public List<CommonVo> getByTaskId(Long taskId) {
        LambdaQueryWrapper<TaskAchieveEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(TaskAchieveEntity::getTaskId, taskId)
                .eq(TaskAchieveEntity::getIsDeleted, 0);
        List<TaskAchieveEntity> list = list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) return null;
        return BeanUtil.copyToList(list, CommonVo.class);
    }


    public TaskAchieveDetailsVo getTaskAchieveDetails(Long taskAchieveId) {
        return this.baseMapper.getTaskAchieveDetails(taskAchieveId);
    }


    @Transactional(rollbackFor = Exception.class)
    public Boolean saveTaskAchieve(TaskAchieveDetailsDto dto) {
        UserVo userVo = UserContext.getUser();
        dto.setUserId(userVo.getId());
        //新增成果
        this.baseMapper.saveTaskAchieve(dto);
        //文件夹及附件
        if (CollectionUtil.isNotEmpty(dto.getAchieveFileVoList())) {
            taskAchieveFileServiceImpl.saveTaskAchieve(dto.getId(), dto.getAchieveFileVoList());
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean updateTaskAchieve(TaskAchieveDetailsDto dto) {
        //删除3
        List<Long> taskAchieveFileIds = taskAchieveFileServiceImpl.getIdByTaskAchieveId(dto.getId());
        if(CollectionUtil.isNotEmpty(taskAchieveFileIds)) {
            taskAchieveFileLinkServiceImpl.deleteTaskAchieve(taskAchieveFileIds);
        }
        //删除2
        taskAchieveFileServiceImpl.removeByTaskAchieveId(dto.getId());
        //文件夹及附件
        if (CollectionUtil.isNotEmpty(dto.getAchieveFileVoList())) {
            taskAchieveFileServiceImpl.updateTaskAchieve(dto.getId(), dto.getAchieveFileVoList());
        }
        //删除1
        return this.updateById(BeanUtil.copyProperties(dto, TaskAchieveEntity.class));
    }


    public Boolean deleteTaskAchieve(Long taskAchieveId) {
        taskAchieveFileServiceImpl.deleteTaskAchieve(taskAchieveId);
        return this.removeById(taskAchieveId);
    }


}
